package com.sskj.run.ui.fragment;


import android.support.design.widget.BottomSheetDialog;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.hey.lib.HeySpinner;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.BottomSheetUtil;
import com.sskj.run.R;
import com.sskj.run.R2;
import com.sskj.run.bean.BetEnum;
import com.sskj.run.bean.CoinTypeBean;
import com.sskj.run.bean.ZhanjiBean;
import com.sskj.run.component.DaggerUserDataComponent;
import com.sskj.run.presenter.UserInfoFragmentPresenter;

import java.util.Arrays;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import io.reactivex.Flowable;

@Route(path = RConfig.RUN_FRAGMENT_USER_INFO)//用户信息
public class UserInfoFragment extends BaseFragment<UserInfoFragmentPresenter> {
    @Inject
    UserViewModel userViewModel;
    @BindView(R2.id.spinner)
    HeySpinner spinner;


    @BindView(R2.id.tv_now_profit)
    TextView tvNowProfit;
    @BindView(R2.id.tv_bet_total_money)
    TextView tvBetTotalMoney;
    @BindView(R2.id.tv_tallest_profit)
    TextView tvTallestProfit;
    @BindView(R2.id.tv_tallest_loss)
    TextView tvTallestLoss;
    @BindView(R2.id.tv_profit_rank)
    TextView tvProfitRank;
    @BindView(R2.id.tv_bet_times)
    TextView tvBetTimes;
    @BindView(R2.id.tv_register_data)
    TextView tvRegisterData;
    @BindView(R2.id.tv_nick_name)
    TextView tvNickName;
    private UserData userData;
    private BottomSheetDialog bottomSheetType;

    @Override
    protected int getLayoutId() {
        return R.layout.run_activity_user_info;
    }

    @Override
    public UserInfoFragmentPresenter getPresenter() {
        return new UserInfoFragmentPresenter();
    }

    @Override
    protected void initView() {
        mPresenter.getUserInfo(BetEnum.getDefault().getType());
        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            } else {
                userData = null;
            }
        });
    }

    @Override
    protected void initData() {
        mPresenter.getCoinType();
    }

    public void updateUI(ZhanjiBean zhanjiBean) {

        tvNowProfit.setText(zhanjiBean.getIncome());
        tvBetTotalMoney.setText(zhanjiBean.getLeiji());
        tvTallestProfit.setText(zhanjiBean.getZuigao());
        tvTallestLoss.setText(zhanjiBean.getLoss());
        tvProfitRank.setText(zhanjiBean.getRanking());
        tvBetTimes.setText(zhanjiBean.getNum());
        tvNickName.setText(zhanjiBean.getRealname());
        tvRegisterData.setText(App.INSTANCE.getString(R.string.run_userInfoFragment1) + zhanjiBean.getReg_time());
//        if (!TextUtils.isEmpty(zhanjiBean.getReg_time())) {
//            tvRegisterData.setText(App.INSTANCE.getString(R.string.run_userInfoFragment1) + TimeFormatUtil.SF_FORMAT_F.format(new Date(Long.valueOf(Long.valueOf(zhanjiBean.getReg_time()) * 1000))));
//        }
    }

    public void initSpinner(List<CoinTypeBean> coinTypeBeans) {
        Flowable.fromIterable(coinTypeBeans)
                .filter(coinTypeBean -> coinTypeBean.getIs_game() == 1)
                .map(coinTypeBean -> coinTypeBean.getCode())
                .toList()
                .subscribe((strings, throwable) -> {
                    List<String> payTypes = strings;
                    spinner.setText(payTypes.get(0));
                    mPresenter.getUserInfo(payTypes.get(0).toLowerCase());//金币
                    ClickUtil.click(spinner, () -> {
                        if (bottomSheetType == null) {
                            bottomSheetType = BottomSheetUtil.getBottomSheet(getActivity(), null, (recyclerView, i, view) -> {
                                bottomSheetType.dismiss();
                                spinner.setText(payTypes.get(i));
                                mPresenter.getUserInfo(payTypes.get(i).toLowerCase());//金币
                            }, payTypes);
                        }
                        bottomSheetType.show();
                    });

                });

    }
}
